package com.boho.common;

/**
 * 用于处理文件扩展名与MIME对照的类
 * 
 * @author coca
 */
public class ContentTypeUtils {

	/**
	 * 默认私有构造器.
	 */
	private ContentTypeUtils() {
	}

	protected static final java.util.Hashtable<String, String> contentTypes = new java.util.Hashtable<String, String>();
	protected static final String UNKNOWN_EXT = "[unknown]";
	static {
		contentTypes.put(UNKNOWN_EXT, "application/octet-stream");
		contentTypes.put(".abs", "audio/x-mpeg");
		contentTypes.put(".ai", "application/postscript");
		contentTypes.put(".aif", "audio/x-aiff");
		contentTypes.put(".aifc", "audio/x-aiff");
		contentTypes.put(".aiff", "audio/x-aiff");
		contentTypes.put(".aim", "application/x-aim");
		contentTypes.put(".art", "image/x-jg");
		contentTypes.put(".asf", "video/x-ms-asf");
		contentTypes.put(".asx", "video/x-ms-asf");
		contentTypes.put(".au", "audio/basic");
		contentTypes.put(".avi", "video/x-msvideo");
		contentTypes.put(".avx", "video/x-rad-screenplay");
		contentTypes.put(".bcpio", "application/x-bcpio");
		contentTypes.put(".bin", "application/octet-stream");
		contentTypes.put(".bmp", "image/bmp");
		contentTypes.put(".body", "text/html");
		contentTypes.put(".cdf", "application/x-cdf");
		contentTypes.put(".cer", "application/x-x509-ca-cert");
		contentTypes.put(".class", "application/java");
		contentTypes.put(".cpio", "application/x-cpio");
		contentTypes.put(".csh", "application/x-csh");
		contentTypes.put(".css", "text/css");
		contentTypes.put(".dib", "image/bmp");
		contentTypes.put(".doc", "application/msword");
		contentTypes.put(".docx", "application/msword");
		contentTypes.put(".dtd", "text/plain");
		contentTypes.put(".dv", "video/x-dv");
		contentTypes.put(".dvi", "application/x-dvi");
		contentTypes.put(".eps", "application/postscript");
		contentTypes.put(".etx", "text/x-setext");
		contentTypes.put(".exe", "application/octet-stream");
		contentTypes.put(".gif", "image/gif");
		contentTypes.put(".gtar", "application/x-gtar");
		contentTypes.put(".gz", "application/x-gzip");
		contentTypes.put(".hdf", "application/x-hdf");
		contentTypes.put(".hqx", "application/mac-binhex40");
		contentTypes.put(".htc", "text/x-component");
		contentTypes.put(".htm", "text/html");
		contentTypes.put(".html", "text/html");
		contentTypes.put(".ief", "image/ief");
		contentTypes.put(".jad", "text/vnd.sun.j2me.app-descriptor");
		contentTypes.put(".jar", "application/java-archive");
		contentTypes.put(".java", "text/plain");
		contentTypes.put(".jnlp", "application/x-java-jnlp-file");
		contentTypes.put(".jpe", "image/jpeg");
		contentTypes.put(".jpeg", "image/jpeg");
		contentTypes.put(".jpg", "image/jpeg");
		contentTypes.put(".js", "text/javascript");
		contentTypes.put(".jsf", "text/plain");
		contentTypes.put(".jspf", "text/plain");
		contentTypes.put(".kar", "audio/x-midi");
		contentTypes.put(".latex", "application/x-latex");
		contentTypes.put(".m3u", "audio/x-mpegurl");
		contentTypes.put(".mac", "image/x-macpaint");
		contentTypes.put(".man", "application/x-troff-man");
		contentTypes.put(".me", "application/x-troff-me");
		contentTypes.put(".mid", "audio/x-midi");
		contentTypes.put(".midi", "audio/x-midi");
		contentTypes.put(".mif", "application/x-mif");
		contentTypes.put(".mov", "video/quicktime");
		contentTypes.put(".movie", "video/x-sgi-movie");
		contentTypes.put(".mp1", "audio/x-mpeg");
		contentTypes.put(".mp2", "audio/x-mpeg");
		contentTypes.put(".mp3", "audio/x-mpeg");
		contentTypes.put(".mpa", "audio/x-mpeg");
		contentTypes.put(".mpe", "video/mpeg");
		contentTypes.put(".mpeg", "video/mpeg");
		contentTypes.put(".mpega", "audio/x-mpeg");
		contentTypes.put(".mpg", "video/mpeg");
		contentTypes.put(".mpv2", "video/mpeg2");
		contentTypes.put(".ms", "application/x-wais-source");
		contentTypes.put(".nc", "application/x-netcdf");
		contentTypes.put(".oda", "application/oda");
		contentTypes.put(".pbm", "image/x-portable-bitmap");
		contentTypes.put(".pct", "image/pict");
		contentTypes.put(".pdf", "application/pdf");
		contentTypes.put(".pgm", "image/x-portable-graymap");
		contentTypes.put(".pic", "image/pict");
		contentTypes.put(".pict", "image/pict");
		contentTypes.put(".pls", "audio/x-scpls");
		contentTypes.put(".png", "image/png");
		contentTypes.put(".pnm", "image/x-portable-anymap");
		contentTypes.put(".pnt", "image/x-macpaint");
		contentTypes.put(".ppm", "image/x-portable-pixmap");
		contentTypes.put(".ppt", "application/mspowerpoint");
		contentTypes.put(".ps", "application/postscript");
		contentTypes.put(".psd", "image/x-photoshop");
		contentTypes.put(".qt", "video/quicktime");
		contentTypes.put(".qti", "image/x-quicktime");
		contentTypes.put(".qtif", "image/x-quicktime");
		contentTypes.put(".ras", "image/x-cmu-raster");
		contentTypes.put(".rgb", "image/x-rgb");
		contentTypes.put(".rm", "application/vnd.rn-realmedia");
		contentTypes.put(".roff", "application/x-troff");
		contentTypes.put(".rtf", "application/rtf");
		contentTypes.put(".rtx", "text/richtext");
		contentTypes.put(".sh", "application/x-sh");
		contentTypes.put(".shar", "application/x-shar");
		contentTypes.put(".sit", "application/x-stuffit");
		contentTypes.put(".smf", "audio/x-midi");
		contentTypes.put(".snd", "audio/basic");
		contentTypes.put(".src", "application/x-wais-source");
		contentTypes.put(".sv4cpio", "application/x-sv4cpio");
		contentTypes.put(".sv4crc", "application/x-sv4crc");
		contentTypes.put(".swf", "application/x-shockwave-flash");
		contentTypes.put(".t", "application/x-troff");
		contentTypes.put(".tar", "application/x-tar");
		contentTypes.put(".tcl", "application/x-tcl");
		contentTypes.put(".tex", "application/x-tex");
		contentTypes.put(".texi", "application/x-texinfo");
		contentTypes.put(".texinfo", "application/x-texinfo");
		contentTypes.put(".tif", "image/tiff");
		contentTypes.put(".tiff", "image/tiff");
		contentTypes.put(".tr", "application/x-troff");
		contentTypes.put(".tsv", "text/tab-separated-values");
		contentTypes.put(".txt", "text/plain");
		contentTypes.put(".ulw", "audio/basic");
		contentTypes.put(".ustar", "application/x-ustar");
		contentTypes.put(".xbm", "image/x-xbitmap");
		contentTypes.put(".xls", "application/msexcel");
		contentTypes.put(".xml", "text/xml");
		contentTypes.put(".xpm", "image/x-xpixmap");
		contentTypes.put(".xsl", "text/xml");
		contentTypes.put(".xwd", "image/x-xwindowdump");
		contentTypes.put(".wav", "audio/x-wav");
		contentTypes.put(".svg", "image/svg+xml");
		contentTypes.put(".svgz", "image/svg+xml");
		contentTypes.put(".wbmp", "image/vnd.wap.wbmp");
		contentTypes.put(".wps", "application/kswps");
		contentTypes.put(".wml", "text/vnd.wap.wml");
		contentTypes.put(".wmlc", "application/vnd.wap.wmlc");
		contentTypes.put(".wmls", "text/vnd.wap.wmlscript");
		contentTypes.put(".wmlscrip", "tcapplication/vnd.wap.wmlscriptc");
		contentTypes.put(".wrl", "x-world/x-vrml");
		contentTypes.put(".Z", "application/x-compress");
		contentTypes.put(".z", "application/x-compress");
		contentTypes.put(".zip", "application/zip");
	}

	/**
	 * 根据文件名或者扩展名返回MIME内容类型(文件在Web上上传下载时的content-type值)。
	 * 
	 * @param ext
	 *            String 文件名（包含扩展名）或者直接传入扩展名。
	 * @return String 返回对应的MIME类型，如传入“doc”则返回“application/msword”。
	 */
	public static String getContentType(String ext) {
		if (ext == null || ext.length() == 0)
			return contentTypes.get(UNKNOWN_EXT);
		String tmp = null;
		if (ext.startsWith("."))
			tmp = ext;
		else {
			int pos = ext.lastIndexOf(".");
			if (pos > 0)
				tmp = ext.substring(pos);
			else
				tmp = "." + ext;
		}
		String ret = contentTypes.get(tmp.toLowerCase());
		if (ret == null)
			return contentTypes.get(UNKNOWN_EXT);
		else
			return ret;
	}

	/**
	 * 根据MIME内容类型(文件在Web上上传下载时的content-type值)获取对应文件扩展。
	 * 
	 * @param contentTypeName
	 *            String 文件名（包含扩展名）或者直接传入扩展名。
	 * @return String 返回对应的扩展名，如传入“application/msword”则返回“.doc”。
	 */
	public static String getExtendName(String contentTypeName) {
		if (contentTypeName == null || contentTypeName.length() == 0)
			return null;
		for (String k : contentTypes.keySet()) {
			if (contentTypeName == contentTypes.get(k))
				return k;
		}
		return null;
	}

}
